/**
 *	@project:	kybernetes
 *	@author:	D.Toepel
 *	@note:		---
 *	
 *	Copyright (C) 2009  D.Toepel <de.dtools> {at} <gmail> {dot} <com>
 *	
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *	
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *	
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 */
package dt.htwk.core;


import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.net.URL;
import java.util.Hashtable;
import java.util.Timer;

import javax.help.HelpSet;
import javax.help.JHelp;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import dt.htwk.extras.LanguageTools;
import dt.htwk.panels.KCAdminPanel;
import dt.htwk.panels.KCRefereePanel;
import dt.htwk.panels.KCToolPanel;
import dt.htwk.panels.KCUserPanel;
import dt.htwk.panels.RegattaView;
import dt.htwk.res.Resource;
import dt.htwk.rmi.KCSecurityManager;
import dt.htwk.sql.Database;
import dt.htwk.utilities.NaviModel;
import dt.htwk.utilities.NaviNode;
import dt.htwk.utilities.RoundedTitleBorder;

/**
 * <h3>Kybernetes</h3>
 * Hauptfenster der Anwendung
 * @author DToepel
 *
 */
public class Kybernetes extends JFrame{

/* ****************************************************
 * Deklaration der Variablen und Komponenten
 * ***************************************************/

	/**
	 * serialVersionUID -- Standard: 1L
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * 
	 */
	private static Hashtable<String,String> lng;
	
	/**
	 * 
	 */
	private static Database iDB;
	
	/*
	 * MenuBar - Komponenten
	 */
	private JMenuBar menuBar;
	private JMenu menu;
	private JMenuItem menuItem;
	
	private KCTimer kcTimer;
	private Timer time;
	private KCStatusBar kcStatusBar;

	/*
	 * Komponenten der Hauptansicht
	 */
	private JPanel mainPanel;
	
	private JPanel startPanel;
	public static String StartPanel = "StartPanel";
	
	private JPanel adminPanel;
	public static String AdminPanel = "AdminPanel";
	private KCAdminPanel kcAdminPanel;
	
	private JPanel refereePanel;
	public static String RefereePanel = "RefereePanel";
	private KCRefereePanel kcRefereePanel;
	
	private JPanel userPanel;
	public static String UserPanel = "UserPanel";
	private KCUserPanel kcUserPanel;
	
	private JPanel guestPanel;
	public static String GuestPanel = "GuestPanel";
	
	private JPanel toolPanel;
	public static String ToolPanel = "ToolPanel";
	private KCToolPanel kcToolPanel;
	
	private JPanel helpPanel;
	public static String HelpPanel = "HelpPanel";

	/*
	 * Komponenten der Navigation
	 */
	private JPanel naviPanel;


	
	
/* ****************************************************
 * Aufruf des Konstruktors
 * ***************************************************/

	/**
	 * 
	 */
	public Kybernetes(){
		super();
		initGUI();
	}

		
/* ****************************************************
 * PUBLIC - oeffentliche Methoden
 * ***************************************************/

	/**
	 * <h3>setLanguageContent</h3>
	 *
	 * @param lng
	 */
	public static void setLanguageContent(Hashtable<String,String> lng){
		Kybernetes.lng = lng;
	}

	/**
	 * <h3>setDatabase</h3>
	 *
	 * @param iDB
	 */
	public static void setDatabase(Database iDB) {
		Kybernetes.iDB = iDB;
	}

		

/* ****************************************************
 * PRIVATE - nicht-oeffentliche Methoden
 * ***************************************************/

	/**
	 * <h3>initGUI</h3>
	 *
	 */
	private void initGUI(){
		try {
			this.addComponentListener(new ComponentListener() {
				
				@Override
				public void componentShown(ComponentEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void componentResized(ComponentEvent e) {
					JFrame frame = (JFrame) e.getSource();
					Dimension d = frame.getSize();
					KCStatusBar.setStatusMessage("X: " + d.getWidth() + " Y: " + d.getHeight());
				}
				
				@Override
				public void componentMoved(ComponentEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void componentHidden(ComponentEvent e) {
					// TODO Auto-generated method stub
					
				}
			});
			System.getProperties().remove("java.rmi.server.hostname");
			this.setDefaultCloseOperation(EXIT_ON_CLOSE);
			this.setSize(1024, 768);
//			this.setMinimumSize(new Dimension(1000,720));
			this.setLayout(new BorderLayout());
			this.setTitle(lng.get("app-title"));
			
			this.add(getKCMenuBar(), BorderLayout.PAGE_START);
//			this.add(getKCNaviPanel(), BorderLayout.WEST);
			this.add(getKCMainPanel(), BorderLayout.CENTER);
			this.add(getKCStatusBar(), BorderLayout.PAGE_END);
			
			this.addWindowListener(new WindowListener() {
				
				@Override
				public void windowClosing(WindowEvent e) {
					time.cancel();
					KCStartup.appUnlock();
				}
				
				@Override
				public void windowActivated(WindowEvent e) {
					kcTimer = new KCTimer(kcStatusBar,0);
					time = new Timer();
					time.schedule(kcTimer, 0, 1000);
				}

				@Override
				public void windowClosed(WindowEvent arg0) {}

				@Override
				public void windowDeactivated(WindowEvent arg0) {}

				@Override
				public void windowDeiconified(WindowEvent arg0) {}

				@Override
				public void windowIconified(WindowEvent arg0) {}

				@Override
				public void windowOpened(WindowEvent arg0) {}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * <h3>getKCStatusBar</h3>
	 *
	 * @return
	 */
	private KCStatusBar getKCStatusBar() {
		if(kcStatusBar == null){
			kcStatusBar = new KCStatusBar();
		}
		return kcStatusBar;
	}


	/**
	 * <h3>getKCMainPanel</h3>
	 *
	 * @return
	 */
	private JPanel getKCMainPanel() {
		if (mainPanel == null){
			mainPanel = new JPanel(new CardLayout());
			mainPanel.setBorder(BorderFactory.createEmptyBorder(2,1,2,2));
			
			// Startansicht
			startPanel = new JPanel();//new BorderLayout()
			{
				startPanel.setForeground(Resource.nimbusGray);
				startPanel.setFont(new Font("Verdana",1,12));
				startPanel.setBorder(new RoundedTitleBorder(
						0,lng.get("sp-title"), Resource.darkMarineBlue, Resource.lightMarineBlue));
				mainPanel.add(startPanel, StartPanel);
			}
			
			// Administratoransicht
			adminPanel = new JPanel(new BorderLayout());
			{
				adminPanel.setForeground(Resource.nimbusGray);
				adminPanel.setFont(new Font("Verdana",1,12));
				adminPanel.setBorder(new RoundedTitleBorder(
						0,lng.get("ap-title"), Resource.darkMarineBlue, Resource.lightMarineBlue));
				mainPanel.add(adminPanel, AdminPanel);
				kcAdminPanel = new KCAdminPanel(lng);
				adminPanel.add(kcAdminPanel);
			}
			
			// Schiedsrichteransicht
			refereePanel = new JPanel(new BorderLayout());
			{
				refereePanel.setForeground(Resource.nimbusGray);
				refereePanel.setFont(new Font("Verdana",1,12));
				refereePanel.setBorder(new RoundedTitleBorder(
						0,lng.get("rp-title"), Resource.darkMarineBlue, Resource.lightMarineBlue));
				mainPanel.add(refereePanel, RefereePanel);
				kcRefereePanel = new KCRefereePanel(lng);
				refereePanel.add(kcRefereePanel);
			}
			
			// Teilnehmeransicht
			userPanel = new JPanel(new BorderLayout());
			{
				userPanel.setForeground(Resource.nimbusGray);
				userPanel.setFont(new Font("Verdana",1,12));
				userPanel.setBorder(new RoundedTitleBorder(
						0,lng.get("up-title"), Resource.darkMarineBlue, Resource.lightMarineBlue));
				mainPanel.add(userPanel, UserPanel);
				kcUserPanel = new KCUserPanel(lng);
				userPanel.add(kcUserPanel);
//				initUserPanel();
			}
			
			// Gast- / Beobachteransicht
			guestPanel = new JPanel(new BorderLayout());
			{
				guestPanel.setForeground(Resource.nimbusGray);
				guestPanel.setFont(new Font("Verdana",1,12));
				guestPanel.setBorder(new RoundedTitleBorder(
						0,lng.get("gp-title"), Resource.darkMarineBlue, Resource.lightMarineBlue));
				mainPanel.add(guestPanel, GuestPanel);
				initGuestPanel();
			}
			
			// Extras
			toolPanel = new JPanel(new BorderLayout());
			{
				toolPanel.setForeground(Resource.nimbusGray);
				toolPanel.setFont(new Font("Verdana",1,12));
				toolPanel.setBorder(new RoundedTitleBorder(
						0,lng.get("tp-title"), Resource.darkMarineBlue, Resource.lightMarineBlue));
				mainPanel.add(toolPanel, ToolPanel);
				kcToolPanel = new KCToolPanel(lng);
				toolPanel.add(kcToolPanel);
			}
			
			// Hilfeseiten
			helpPanel = new JPanel(new BorderLayout());
			{
				helpPanel.setForeground(Resource.nimbusGray);
				helpPanel.setFont(new Font("Verdana",1,12));
				helpPanel.setBorder(new RoundedTitleBorder(
						0,lng.get("hp-title"), Resource.darkMarineBlue, Resource.lightMarineBlue));
				mainPanel.add(helpPanel, HelpPanel);
				initHelpPanel();
			}
			
		}
		return mainPanel;
	}


	/**
	 * <h3>getKCNaviPanel</h3>
	 *
	 * @return
	 */
	private JPanel getKCNaviPanel() {
		if (naviPanel == null){
			JPanel panel = new JPanel();
			panel.setPreferredSize(new Dimension(160,500));
			panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
			panel.setBorder(BorderFactory.createEmptyBorder(2,2,2,1));
			
			
			// Navigation
			naviPanel = new JPanel(new BorderLayout());
			{
				naviPanel.setForeground(Resource.nimbusGray);
				naviPanel.setFont(new Font("Verdana",1,12));
				naviPanel.setBorder(new RoundedTitleBorder(
						0,lng.get("np-title"), Resource.darkMarineBlue, Resource.lightMarineBlue));
				panel.add(naviPanel);
				initNaviPanel();
			}
			
			return panel;
		}
		return null;
	}


	private JMenuBar getKCMenuBar(){
		if(menuBar == null){
			menuBar = new JMenuBar();
			{
				menu = new JMenu(lng.get("mb-start"));
				{
					menuItem = new JMenuItem(lng.get("mb-start-login"));
					menuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0) {
							showCard(StartPanel);
						}
					});
					menu.add(menuItem);
					
					menuItem = new JMenuItem(lng.get("mb-start-guest"));
					menuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0) {
							showCard(GuestPanel);
						}
					});
					menu.add(menuItem);
					
					menuItem = new JMenuItem(lng.get("mb-start-exit"));
					menuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0) {
							//TODO dialog save before quit
							System.exit(0);
						}
					});
					menu.add(menuItem);
				}
				menuBar.add(menu);
				
				menu = new JMenu(lng.get("mb-admin"));
				{
					menuItem = new JMenuItem(lng.get("mb-admin-prefs"));
					menuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0) {
							showCard(AdminPanel);
							kcAdminPanel.showCard(KCAdminPanel.ServerSettings);
						}
					});
					menu.add(menuItem);
					
					menuItem = new JMenuItem(lng.get("mb-admin-user"));
					menuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0) {
							showCard(AdminPanel);
							kcAdminPanel.showCard(KCAdminPanel.UserGroups);
						}
					});
					menu.add(menuItem);
					
				}
				menuBar.add(menu);
				
				menu = new JMenu(lng.get("mb-referee"));
				{
					menuItem = new JMenuItem(lng.get("mb-referee-user"));
					menuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0) {
							showCard(RefereePanel);
							kcRefereePanel.showCard(KCRefereePanel.UserPanel);
						}
					});
					menu.add(menuItem);
					
					menuItem = new JMenuItem(lng.get("mb-referee-race"));
					menuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0) {
							showCard(RefereePanel);
							kcRefereePanel.showCard(KCRefereePanel.PrefPanel);
						}
					});
					menu.add(menuItem);
					
					menuItem = new JMenuItem(lng.get("mb-referee-view"));
					menuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0) {
							showCard(RefereePanel);
							kcRefereePanel.showCard(KCRefereePanel.RegattaPanel);
						}
					});
					menu.add(menuItem);
				}
				menuBar.add(menu);
				
				
				menu = new JMenu(lng.get("mb-user"));
				{
					menuItem = new JMenuItem(lng.get("mb-user-param"));
					menuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0) {
							showCard(UserPanel);
							kcUserPanel.showCard(KCUserPanel.ParamPanel);
						}
					});
					menu.add(menuItem);
					
					menuItem = new JMenuItem(lng.get("mb-user-view"));
					menuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0) {
							showCard(UserPanel);
							kcUserPanel.showCard(KCUserPanel.RegattaPanel);
						}
					});
					menu.add(menuItem);
				}
				menuBar.add(menu);
				

				menu = new JMenu(lng.get("mb-tools"));
				{
					menuItem = new JMenuItem(lng.get("mb-tools-archiv"));
					menuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0) {
							showCard(ToolPanel);
							kcToolPanel.showCard(KCToolPanel.ArchivPanel);
						}
					});
					menu.add(menuItem);
										
					menuItem = new JMenuItem(lng.get("mb-tools-sim"));
					menuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0) {
							showCard(ToolPanel);
							kcToolPanel.showCard(KCToolPanel.Simulation);
						}
					});
					menu.add(menuItem);
					
					menuItem = new JMenuItem(lng.get("mb-tools-lng"));
					menuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0) {
							showCard(ToolPanel);
							kcToolPanel.showCard(KCToolPanel.LngPanel);
						}
					});
					menu.add(menuItem);
				}
				menuBar.add(menu);
				
				menu = new JMenu(lng.get("mb-help"));
				{
					menuItem = new JMenuItem(lng.get("mb-help-prog"));
					menuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0) {
							showCard(HelpPanel);
						}
					});
					menu.add(menuItem);
					
					menuItem = new JMenuItem(lng.get("mb-help-rules"));
					menuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0) {
							showCard(HelpPanel);
						}
					});
					menu.add(menuItem);
					
					menuItem = new JMenuItem(lng.get("mb-help-dev"));
					menuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0) {
							showCard(HelpPanel);
						}
					});
					menu.add(menuItem);
					
					menuItem = new JMenuItem(lng.get("mb-help-sim"));
					menuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0) {
							showCard(HelpPanel);
						}
					});
					menu.add(menuItem);
					
					menuItem = new JMenuItem(lng.get("mb-help-about"));
					menuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0) {
							showCard(HelpPanel);
						}
					});
					menu.add(menuItem);
					
				}
				menuBar.add(menu);
				
				
			}
		}
		
		return menuBar;
	}
	
	
	private void showCard(String string){
		CardLayout cl = (CardLayout)(mainPanel.getLayout());
		cl.show(mainPanel, string);
//		if(string.equals(HelpPanel)){
//			naviPanel.getParent().setVisible(false);
//		}else{
//			naviPanel.getParent().setVisible(true);
//		}
	}
	
	private void initNaviPanel(){
		NaviModel.setLanguageContent(lng);
		final NaviTree naviTree = new NaviTree(new NaviModel(new NaviNode("test", "admin")));
		naviTree.setNaviComponents(0);
		naviPanel.add(naviTree, BorderLayout.CENTER);
		naviTree.addTreeSelectionListener(new TreeSelectionListener() {
			
			@Override
			public void valueChanged(TreeSelectionEvent e) {
				
				NaviNode node = (NaviNode)naviTree.getLastSelectedPathComponent();
				
				TreePath path = naviTree.getSelectionModel().getSelectionPath();
				if (path != null) {
					for (int i = 0; i < path.getPathCount(); i++) {
						node = (NaviNode) path.getPathComponent(i);
						System.out.println(node.getActionCommand());
					}
				}


//				System.out.println(node.getActionCommand());
			}
		});
	}
	
	private void initGuestPanel(){
		RegattaView view = new RegattaView(lng, "guest");
		guestPanel.add(view, BorderLayout.CENTER);
	}
	
	private void initHelpPanel(){
		String langID = "de";
		String helpID = "index";
		JHelp helpViewer = null;
		try {
			ClassLoader cl = getClass().getClassLoader();
			URL url = HelpSet.findHelpSet(cl, "dt/htwk/res/hlp/Kybernetes Hilfe.hs");
//			URL url = HelpSet.findHelpSet(cl, "dt/htwk/res/hlp/" + langID + "/main.hs");
			helpViewer = new JHelp(new HelpSet(cl, url));
			// Darzustellendes Kapitel festlegen, ID muss im XML existieren!
			helpViewer.setCurrentID(helpID);
		} catch (Exception e) {
			System.err.println("API Help Set not found");
		}
		helpPanel.add(helpViewer, BorderLayout.CENTER);
	}
	
}
